#include "inputform.h"
#include "ui_inputform.h"
#include <QRandomGenerator>

InputForm::InputForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InputForm)
{
    ui->setupUi(this);

    this->setWindowFlag(Qt::WindowMaximizeButtonHint, false);
    this->menu_bar = new QMenuBar(this);
    this->menu = new QMenu(this->menu_bar);
    this->menu_bar->addMenu(this->menu);
    this->menu_bar->show();

    int w = this->width(), h = this->height();
    this->resize(w, h);

    this->show_tip = false;
    this->ui->tip->hide();
}

InputForm::~InputForm()
{
    delete this->menu;
    delete this->menu_bar;
    delete ui;
}

void InputForm::showEvent(QShowEvent *) {
    if (this->text_list.length() > 0) {
        this->rand = QRandomGenerator::global()->bounded(this->text_list.length());
        this->ui->title->setText(this->text_list.at(this->rand));
        this->ui->tip->setText(this->romaji_list.at(this->rand));
    }
}

void InputForm::resizeEvent(QResizeEvent *event) {
    QSize size = event->size();
    this->resize(size.width(), size.height());
}

void InputForm::keyReleaseEvent(QKeyEvent *event) {
    int key = event->key();
    if (key == Qt::Key_Back) {
        emit this->closeInputForm();
        this->close();
    }
}

void InputForm::closeEvent(QCloseEvent *) {
    emit this->closeInputForm();
}

void InputForm::resize(int w, int h) {
    this->ui->title->setGeometry(0, 0, w, h / 4);
    this->ui->input_line_edit->setGeometry(0, h / 4, w, h / 4);
    this->ui->toggle_tip->setGeometry(0, h / 4 * 2, w, h / 4);
    this->ui->tip->setGeometry(0, h / 4 * 3, w, h / 4);
}

void InputForm::setTextList(QList<QString> text_list) {
    this->text_list = text_list;
}

void InputForm::setRomaji(QList<QString> romaji_list) {
    this->romaji_list = romaji_list;
}

void InputForm::on_toggle_tip_clicked()
{
    this->show_tip = !this->show_tip;
    if (this->show_tip) {
        this->ui->tip->show();
    } else {
        this->ui->tip->hide();
    }
}

void InputForm::on_input_line_edit_textEdited(const QString &arg1)
{
    if (this->romaji_list.at(this->rand) == arg1) {
        this->rand = QRandomGenerator::global()->bounded(this->text_list.length());
        this->ui->title->setText(this->text_list.at(this->rand));
        this->ui->input_line_edit->setText("");
        this->ui->tip->setText(this->romaji_list.at(this->rand));
        this->show_tip = false;
        this->ui->tip->hide();
    }
}

